//
//  UILabel+ZJIconfont.m
//  IconfontDemo
//
//  Created by ZJ on 2018/7/20.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "UILabel+ZJIconfont.h"

static const CGFloat kDefaultWidth = 21;

@implementation UILabel (ZJIconfont)

- (instancetype)initWithFrame:(CGRect)frame unicode:(NSString *)code textColor:(UIColor *)textColor backgroundColor:(UIColor *)backgroundColor iconfontSize:(CGFloat)size
{
    frame = [self sizeIsZero:frame.size] ? CGRectMake(frame.origin.x, frame.origin.y, kDefaultWidth, kDefaultWidth) : frame;
    self = [super initWithFrame:frame];
    if (self) {
        size = size ? size : kDefaultWidth;
        CGFloat iconfontMaxSize = MIN(frame.size.width, frame.size.height);
        iconfontMaxSize = MIN(iconfontMaxSize, size);
        
        self.font = [UIFont fontWithName:@"iconfont" size:iconfontMaxSize];
        
        NSMutableAttributedString *attributeText = [[NSMutableAttributedString alloc] initWithString:code];
        NSDictionary *dict = @{NSForegroundColorAttributeName: textColor ? textColor : [UIColor darkGrayColor],
                               NSBackgroundColorAttributeName: backgroundColor ? backgroundColor : [UIColor clearColor]
                               };
        [attributeText addAttributes:dict range:NSMakeRange(0, attributeText.length)];
        self.attributedText = attributeText;
//        self.text = code;
//        self.textColor = textColor;
        self.textAlignment = NSTextAlignmentCenter;
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (BOOL)sizeIsZero:(CGSize)size {
    return (size.width == CGSizeZero.width) && (size.height == CGSizeZero.height);
}

@end
